<template>
    <div class="container">
        <Form :model="searchObj" inline>
            <FormItem>
                <Input v-model="searchObj.category" class="search-input" clearable placeholder="请输入类别名称"/>
            </FormItem>
            <FormItem>
                <Input v-model="searchObj.author" class="search-input" clearable placeholder="请输入作者名称"/>
            </FormItem>
            <FormItem>
                <Button class="search-btn" type="success" @click="search">
                    <Icon type="ios-search"/>
                    搜索
                </Button>
            </FormItem>
            <FormItem>
                <Button class="search-btn" type="primary" @click="addBtn">
                    <Icon type="ios-add"/>
                    添加类别
                </Button>
            </FormItem>
            <FormItem>
                <Button class="search-btn" type="error" @click="delSelect">
                    <Icon type="ios-remove"/>
                    批量删除
                </Button>
            </FormItem>
        </Form>
        <Row type="flex" justify="center">
            <Col span="24">
                <Table border :loading="tableLoading" :columns="tableColumns" :data="tableData" @on-selection-change="tableSelectChange">
                    <template slot="action" slot-scope="{ row }">
                        <Button type="primary" size="small" class="action-btn" @click="editBtn(row)">编辑</Button>
                        <Button type="info" size="small" class="action-btn" @click="infoBtn(row)">详情</Button>
                        <Button type="error" size="small" class="action-btn" @click="delBtn(row.id)">删除</Button>
                    </template>
                </Table>
            </Col>
        </Row>
        <br>
        <Row type="flex" justify="end">
            <Page show-total :current="pageObj.pageNo" :page-size="pageObj.pageSize" :total="pageObj.totalRecord" @on-change="getPageNo"/>
        </Row>
        <option-modal ref="optionModal" :modal-title="modalTitle" :modal-show="modalShow" :modal-type="modalType" :dataObj="modalData" @cancelOption="cancelOption" @saveOption="saveOption"></option-modal>
    </div>
</template>

<script>
import {
  getCategoryList,
  deleteCategory,
  deleteCategoryPl
} from '@/api/publish-info'
import OptionModal from './components/option-modal'
export default {
  components: { OptionModal },
  data () {
    return {
      modalShow: false,
      modalType: '',
      modalTitle: '',
      modalData: {},
      tableColumns: [
        {
          type: 'selection',
          width: 70,
          // fixed: 'left',
          align: 'center'
        },
        {
          title: '类别名称',
          key: 'category',
          width: 200,
          align: 'center'
        },
        {
          title: '创建时间',
          key: 'create_time',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('span', this.$common.xeUtils.toDateString(params.row.create_time))
            ])
          }
        },
        {
          title: '操作',
          minWidth: 250,
          slot: 'action',
          align: 'center'
        }
      ],
      tableData: [],
      selectData: [],
      tableLoading: false,
      pageObj: {
        pageNo: 1,
        pageSize: 10,
        totalRecord: 0
      },
      searchObj: {

      }
    }
  },
  props: {},
  mounted () {
    const param = {
      pageNo: this.pageObj.pageNo,
      pageSize: this.pageObj.pageSize
    }
    this.init(param)
  },
  methods: {
    cancelOption () {
      this.modalShow = false
    },
    saveOption () {
      this.modalShow = false
      this.init()
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    },
    getPageNo (pageNo) {
      this.pageObj.pageNo = pageNo
      const param = {
        pageNo: this.pageObj.pageNo,
        pageSize: this.pageObj.pageSize,
        category: this.$common.xeUtils.trim(this.searchObj.category)
      }
      this.init(param)
    },
    tableSelectChange (val) {
      this.selectData = val
    },
    initPage () {
      this.pageObj.pageNo = 1
      this.pageObj.pageSize = 10
    },
    init (data) {
      this.tableLoading = true
      getCategoryList(data).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.tableLoading = false
          this.tableData = data.data.results
          this.pageObj.totalRecord = data.data.totalRecord
        } else {
          this.tableLoading = false
          this.error(data)
        }
      })
    },
    search () {
      this.initPage()
      const param = {
        pageNo: this.pageObj.pageNo,
        pageSize: this.pageObj.pageSize,
        category: this.$common.xeUtils.trim(this.searchObj.category)
      }
      this.init(param)
    },
    addBtn () {
      this.modalTitle = '添加文章'
      this.modalType = 'add'
      this.modalData = {}
      this.modalShow = true
    },
    editBtn (val) {
      this.modalTitle = '编辑文章'
      this.modalType = 'edit'
      this.modalData = this.$common.xeUtils.clone(val)
      this.modalShow = true
    },
    infoBtn (val) {
      this.modalTitle = '文章详情'
      this.modalType = 'info'
      this.modalData = this.$common.xeUtils.clone(val)
      this.modalShow = true
    },
    delSelect () {
      let delList = this.selectData
      let ids = []
      if (delList.length <= 0) {
        this.$Message.error('请选择要删除的数据')
        return false
      }
      delList.forEach((item) => {
        ids.push(item.id)
      })
      this.$Modal.confirm({
        title: '确认删除选中信息吗？',
        content: '删除后不可恢复，请确认后再操作！',
        loading: true,
        onOk: () => {
          this.delMulti(ids)
        }
      })
    },
    delBtn (val) {
      this.$Modal.confirm({
        title: '确认删除此文章么？',
        content: '请仔细核对后再操作',
        loading: true,
        onOk: () => {
          this.deleteOne(val)
        }
      })
    },
    deleteOne (val) {
      deleteCategory({ id: val }).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.$Modal.remove()
          this.init()
          this.success('删除成功')
        } else {
          this.$Modal.remove()
          this.error(data)
        }
      })
    },
    delMulti (ids) {
      deleteCategoryPl({ id: ids }).then(ret => {
        const data = ret.data
        if (data.errcode === 0) {
          this.$Modal.remove()
          this.init()
          this.success('删除成功')
        } else {
          this.$Modal.remove()
          this.error(data)
        }
      })
    }
  }
}
</script>

<style scoped>
    .container {
        padding-bottom: 50px;
    }
    .action-btn {
        margin-right: 5px;
        margin-bottom: 5px;
    }
    .search-input {
        width: 180px;
    }
    .search-btn {
        width: 100px;
    }
</style>
